<?php
namespace App\Http\Controllers\Api\V5;

use App\Common\ApiData;
use App\Modules\EntMindShare;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Validator, DB;
use App\Modules\DefFavorite;
use App\Modules\DefComment;
use App\Modules\DefGood;
use Illuminate\Support\Facades\Input;


class ApiHomeController extends BaseController
{

	/*首页广告图片*/
	public function showImages() {
		$adImgs = DB::table('ad_info')->where('is_delete',0)->pluck("ad_img");
		foreach($adImgs as $key => $img){
			$adImgs[$key] = env('ADMIN_HOST')."/".$img;
		}
		$key = md5(implode('',$adImgs->toArray()));
		return apiData()
			->set_data('key',$key)
			->set_data('images',$adImgs)
			->send(200,'ok');

	}



	/**
	 * 获取热点推荐／自学习社区详情页
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function homedetail(Request $request) {
		$pid    = Input::get('pid', 0);
		$type   = Input::get('type', 0);
		$uid    = Input::get('UserID', 0);


		if($pid && $type) {
			$goodNumber 	= DefGood::where('pid', $pid)->count();//点赞数
			$commentNumber	= DefComment::where('pid', $pid)->count();//评论数
			$isGood 		= 0;
			$isFavorite 	= 0;
			$parentId		= 0;
			if($uid){
				//是否点赞
				$isGood = DefGood::where('pid', $pid)->where('uid', $uid)->where('type', $type)->count();
				$isGood = $isGood?1:0;

				//是否收藏
				$isFavorite = DefFavorite::where('pid', $pid)->where('uid', $uid)->where('type', $type)->count();
				$isFavorite = $isFavorite?1:0;

				//学习官ID
				$memberNumber = DB::table('memberinfo')->where('UserID',$uid)->pluck('MemNumber');
				if($memberNumber){
					$parentId = DB::table('memberdrp')->where('MemNumber',$memberNumber)->value('DRPID');
				}
			}

			if($type == 'STUDY'){  //心灵文章
				$articleInfo =  DB::table('mindshare')->where('MinSgaID',$pid)->first();
				$params 	 = [
					'articleId' =>  $articleInfo->MinSgaID,
					'parentId'     =>  $parentId
				];
				$params 	 = http_build_query($params);
				$shareInfo	 = [
					'title'			=> $articleInfo->MinSgaTitle,
					'description'	=> $articleInfo->MinSgaDesc ? $articleInfo->MinSgaDesc : '教导读书会',
					'img'			=> env('ADMIN_HOST').$articleInfo->FilePath1,
					'link'			=> env('ADMIN_HOST')."/home/mind_share_to_weixin?".$params,
					'groupTitle'	=> $articleInfo->MinSgaTitle
				];

			}elseif($type=='HOT'){  //热门书籍
				$bookInfo   = DB::table('bookhots')->where('BoHoID',$pid)->first();
				$params     = ['bookId'=>$bookInfo->BoHoID,'parentId'=>$parentId];
				$params     = http_build_query($params);
				$shareInfo	 = [
					'title'			=> $bookInfo->BoHoTitle,
					'description'	=> $bookInfo->BoHoDescription ? $bookInfo->BoHoDescription : '教导读书会',
					'img'			=> env('ADMIN_HOST').$bookInfo->FilePath1,
					'link'			=> env('ADMIN_HOST')."/home/hot_book_share_to_weixin?".$params,
					'groupTitle'	=> $bookInfo->BoHoTitle
				];
			}else{
				return apiData()->send(404,'类型type'.$type."未知");
			}

			$data = [
				'good_number'	=> $goodNumber,
				'comment_number'=> $commentNumber,
				'is_good'		=> $isGood,
				'is_favorite'	=> $isFavorite,
				'shareInfo'     => $shareInfo
			];
		} else {
			return apiData()->send(300,'参数错误');
		}
		return apiData()->set_data('data',$data)->send_old(200,'ok');
	}

	/**
	 * 点赞
	 * @param Request $request [description]
	 */
	public function setGood(Request $request) {
		$uid = (int)Input::get('UserID', 0);
		$pid = (int)Input::get('pid', 0);
		$type = (string)Input::get('type', 0);

		try {
			if($uid && $pid && $type) {
				$good = DefGood::where('pid', $pid)->where('uid', $uid)->where('type', $type)->first();
				if($good) {
					DB::table('def_good')->where('pid', $pid)->where('uid', $uid)->where('type', $type)->delete();
				} else {
					$good = new DefGood();
					$good->pid = $pid;
					$good->type = $type;
					$good->uid = $uid;
					$good->save();//点赞
				}
			} else {
				return apiData()->send(300,'参数错误');
			}
			return apiData()->send(200,'ok');
		}
		catch (\Exception $e)
		{
			return apiData()->send($e->getCode(),$e->getMessage());
		}
	}



    /*
     * 保存评论信息
     * */
	public function setComment(Request $request) {
		$uid    = Input::get('UserID', 0);
		$pid    = Input::get('pid', 0);
		$type   = Input::get('type', 0);
        $fid    = Input::get('fid', 0);
		$description = Input::get('description', 0);
        $description = emoji_text_encode($description);
		try {
			if($uid && $pid && $type && $description) {
				$comment = new DefComment();
				$comment->pid = $pid;
				$comment->uid = $uid;
				$comment->type = $type;
				$comment->description = $description;
				$comment->fid = $fid;
				$comment->save();
			} else {
				return apiData()->send(300,'参数错误');
			}
			return apiData()->send(200,'ok');
		} catch (\Exception $e) {

			return apiData()->send($e->getCode(), $e->getMessage());
		}
	}

    /*
     * 获取评论信息
     * */
	public function getComment() {
		$pid 	= Input::get('pid', 0);
		$type 	= Input::get('type', 0);
		$page 	= Input::get('page', 1);
		$page_count = Input::get('page_count', 20);
		if($pid && $type) {
			$page = $page < 1 ? 0 : ($page - 1);
			$skip = $page * $page_count;
			$mindShareModel = new EntMindShare();
			$comment = $mindShareModel->get_article_comment($pid,$skip,$page_count,$type);
		} else {
			return apiData()->send(300,'参数错误');
		}
		return apiData()->set_data('data',$comment)->send_old(200,'ok');
	}


	/**
	 * 获得视频/文章评论和分享信息
	 * */
	public function get_video_or_article_comment_share_info(){
		$pid    = Input::get('pid', 0); 	//内容ID
		$type   = Input::get('type', 0);	//内容类型,文章还是视频还是
		$uid    = Input::get('UserID', 0);	//用户ID
		$page 		= 0;
		$page_count = 20;
		$apiData	= new ApiData();

		$params = [
			'pid'	=> [$pid,'缺少内容ID',2001],
			'type'	=> [$type,'缺少类型ID',2002],
			'uid'	=> [$uid,'缺少用户ID',2003]
		];

		$checkResult = $this->check_params($params);
		if($checkResult->status == false){
			$apiData->message	= $checkResult->message;
			$apiData->code		= $apiData->code;
			return $apiData->send();
		}

		$skip = $page * $page_count;
		$mindShareModel = new EntMindShare();
		$comment = $mindShareModel->get_article_comment($pid,$skip,$page_count,$type);


        $goodNumber     = DefGood::where('pid', $pid)->where('type',$type)->count();//点赞数
        $commentNumber  = DefComment::where('pid', $pid)->where('type',$type)->count();//评论数
		$isGood 		= 0;
		$isFavorite 	= 0;
		$parentId		= 0;
		if($uid){
			//是否点赞
			$isGood = DefGood::where('pid', $pid)->where('uid', $uid)->where('type', $type)->count();
			$isGood = $isGood?1:0;

			//是否收藏
			$isFavorite = DefFavorite::where('pid', $pid)->where('uid', $uid)->where('type', $type)->count();
			$isFavorite = $isFavorite?1:0;

			//学习官ID
			$memberNumber = DB::table('memberinfo')->where('UserID',$uid)->pluck('MemNumber');

			if($memberNumber){
				$parentId = DB::table('memberdrp')->where('MemNumber',$memberNumber)->value('DRPID');
			}
		}

		if($type == 'STUDY'){  //心灵文章
			$articleInfo =  DB::table('mindshare')->where('MinSgaID',$pid)->first();
			$params 	 = [
				'articleId' =>  $articleInfo->MinSgaID,
				'parentId'     =>  $parentId
			];
			$params 	 = http_build_query($params);
			$shareInfo	 = [
				'title'			=> $articleInfo->MinSgaTitle,
				'description'	=> $articleInfo->MinSgaDesc ? $articleInfo->MinSgaDesc : '教导读书会',
				'img'			=> env('ADMIN_HOST').$articleInfo->FilePath1,
				'link'			=> env('ADMIN_HOST')."/home/mind_share_to_weixin?".$params,
				'groupTitle'	=> $articleInfo->MinSgaTitle
			];

		}elseif($type=='HOT'){  //热门书籍
			$bookInfo   = DB::table('bookhots')->where('BoHoID',$pid)->first();
			$params     = ['bookId'=>$bookInfo->BoHoID,'parentId'=>$parentId];
			$params     = http_build_query($params);
			$shareInfo	 = [
				'title'			=> $bookInfo->BoHoTitle,
				'description'	=> $bookInfo->BoHoDescription ? $bookInfo->BoHoDescription : "教导读书会" ,
				'img'			=> env('ADMIN_HOST').$bookInfo->FilePath1,
				'link'			=> env('ADMIN_HOST')."/home/hot_book_share_to_weixin?".$params,
				'groupTitle'	=> $bookInfo->BoHoTitle
			];
		}else{
			return apiData()->send(404,'类型type'.$type."未知");
		}

		$apiData->message = "ok";
		$apiData->code 	= 200;
		return $apiData->set_data('goodNumber',$goodNumber)
			->set_data('commentNumber',$commentNumber)
			->set_data('isGood',$isGood)
			->set_data('isFavorite',$isFavorite)
			->set_data('shareInfo',$shareInfo)
			->set_data('comments',$comment)
			->send();
	}
}